package cn.wolfcode.p2p.base.controller;

import cn.wolfcode.p2p.base.domain.SystemDictionaryItem;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.service.ISystemDictionaryService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.util.JsonResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 前台用户资料相关
 */
@Controller
public class BasicInfoController {
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("basicInfo")
    public String userInfo(Model model) {
        //用户资料
        model.addAttribute("userinfo", userInfoService.getById(UserContext.getLoginInfo().getId()));
        //个人学历
        List<SystemDictionaryItem> educationBackground = systemDictionaryService.listItemBySn("educationBackgrounds");
        List<SystemDictionaryItem> incomeGrade = systemDictionaryService.listItemBySn("incomeGrades");
        List<SystemDictionaryItem> marriage = systemDictionaryService.listItemBySn("marriages");
        List<SystemDictionaryItem> kidCount = systemDictionaryService.listItemBySn("kidCounts");
        List<SystemDictionaryItem> houseCondition = systemDictionaryService.listItemBySn("houseConditions");

        //共享在作用域中
        model.addAttribute("educationBackgrounds", educationBackground);
        model.addAttribute("incomeGrades", incomeGrade);
        model.addAttribute("marriages", marriage);
        model.addAttribute("kidCounts", kidCount);
        model.addAttribute("houseConditions", houseCondition);
        return "userInfo";
    }

    /**
     * 保存个人资料
     * @param userInfo
     * @return
     */
    @RequestMapping("basicInfo_save")
    @ResponseBody
    public JsonResult save(UserInfo userInfo){
        userInfoService.basicInfoSave(userInfo);
        return JsonResult.instance();
    }
}
